<?php
require_once('sub.inc.php');

if( $_SERVER['QUERY_STRING'] !== "" ){
	$code = clean($_SERVER['QUERY_STRING']);
	if(preg_match("/(.*)\-/", $code, $ray)){
		$code = $ray[1];
		$preview = true;
	}
	$res = mysqli_query($GLOBALS['con'], "SELECT `url`,`id` FROM `links` WHERE `code` LIKE BINARY '$code'");
	if( $row = mysqli_fetch_array($res) ){
		if($preview){
			head("Preview");
			print "<html>The URL you are requesting is ";
			if(preg_match("/http:\/\/(.*)\.(.*).org\/wiki\/(.*)/", $row['url'], $ray)){
				print "for the <u>" . langLookup($ray[1]) . "</u> edition of <u>" . ucfirst($ray[2]) . "</u>.  The article is titled [[<a href=\"{$row['url']}\">".cleanUp($ray[3])."</a>]]";
			}else{
				print ": <a href=\"{$row['url']}\">{$row['url']}</a>";
			}
			foot();
		}else{
			header('Location: '. $row['url'], 307);
		}
		
		$res2 = mysqli_query($GLOBALS['con'], "SELECT `id` from `domains` WHERE `tld`='{$_SERVER['SERVER_NAME']}'");
		if( $row2 = mysqli_fetch_array($res2) ){
			$domain = $row2['id'];
		}else{
			$domain = '0';
		}

		mysqli_query($GLOBALS['con'], "INSERT INTO `logs` (`event`, `relatedid`, `message`, `message2`, `ip`, `domain`) VALUES ('HIT', '{$row['id']}', '{$_SERVER['HTTP_USER_AGENT']}', '{$_SERVER['HTTP_REFERER']}', '{$_SERVER['REMOTE_ADDR']}', '$domain')");
	}else{
		print "Sorry, that's not a valid code.";
	}
}else{
head("Home");
?>
<h1>wmf4.me <?php print $GLOBALS['Ver'];?></h1>
<br>URL Shortening for the WMF community. 
<br><br>
<h3>About</h3>
Enwn.net was started by [[<a href="http://en.wikinews.org/wiki/User:ShakataGaNai">User:ShakataGaNai</a>]] for the purpose of posting short links on the 
<a href="http://twitter.com/en_wikinews">English Wikinews twitter</a> automatically from the RSS feed.  
By the suggestion of some of the EnWn users, enwn.net became publicly accessible to all.  After more commentary, wmf4.me was registered to make the site more accessible to all the 
Wikimedia Foundation sites. This site is a <a href="http://en.wikipedia.org/wiki/URL_shortener">URL Shortener</a> for all foundation sites.  With the exception of a few specific authorized 
users, no one can create links to sites outside of the WMF.
<br><br>
<h3>Contact Us</h3>
Few ways to get ahold of us:
<ul><li>[[<a href="http://en.wikinews.org/wiki/User_talk:ShakataGaNai">User talk:ShakataGaNai</a>]] on Wikinews
<li>[[<a href="http://en.wikinews.org/wiki/User:ShakataGaNai/Wmf4.me">User:ShakataGaNai/Wmf4.me</a>]]
<li><u>wiki</u> ta <u>konsoletek</u> tod <u>com</u>  (Email, fix the 'ta' and 'tod')
<li><a href="http://twitter.com/wmf4me">Twitter</a>
<li><a href="http://identi.ca/wmf4me">Identi.ca</a>
</ul>

<br /><br />
<form action="shorten.php" method="post"> 
URL: <input type="text" name="url" size="128" />
Domain: <select name="domain"> <?php

	$res3 = mysqli_query($GLOBALS['con'], "SELECT *  from `domains` WHERE 1");
	while( $row3 = mysqli_fetch_array($res3) ){
		$bonus = null;
		if($row3['default']){
			$bonus = "selected=\"selected\"";
		}
		print "<option value=\"{$row3['id']}\" $bonus>{$row3['tld']}</option>";
	}?>
</select>
<input type="submit" value="Shorten!" />
</form> 

<!--/* OpenX iFrame Tag v2.8.2 */-->
<br><br>
<iframe id='a36cd9a5' name='a36cd9a5' src='http://devtionary.org/open/www/delivery/afr.php?zoneid=3&amp;cb=1482' framespacing='0' frameborder='no' scrolling='no' width='468' height='60'><a href='http://devtionary.org/open/www/delivery/ck.php?n=a2a58d74&amp;cb=INSERT_RANDOM_NUMBER_HERE' target='_blank'><img src='http://devtionary.org/open/www/delivery/avw.php?zoneid=3&amp;cb=INSERT_RANDOM_NUMBER_HERE&amp;n=a2a58d74' border='0' alt='' /></a></iframe>


<?php
foot();
}


?>
